/*
 * bsp_xzc_dyzh.c
 *
 *  Created on: Jul 26, 2022
 *      Author: kaijunHuang
 */

#include "main.h"
#include "bsp_xzc_dyzh.h"
#include "bsp_includes.h"

#if XZC_DYZH_MODULE_ENABLED

DataBuffer dataBuffer[DATABUFFERSIZE]; //市电0~3、取力4~7、绝缘8，充电组件9~10.供电组件1:11-12,供电组件2：13-14,储能输入：15
ErrorBuffer ErrorID[DATABUFFERSIZE];
short temperature = 0;
uint8_t serialNum = 0;
uint16_t communicationErrorFlag;
uint16_t receiveFlag = 0xFFFF;
float power = 0;
DataBuffer dyzhData[UPLOADDATASIZE];

void DataInitForTest(void)
{
	//通讯
	communicationErrorFlag = 0x00;
	//温度
	temperature = 350;
	//市电输入-A相
	dataBuffer[0].data[0] = 0x14;
	dataBuffer[0].data[1] += 1;
	dataBuffer[0].data[2] = 0x98;
	dataBuffer[0].data[3] = 0x08;
	dataBuffer[0].data[4] = 0x01;
	dataBuffer[0].data[5] = 0x00;
	dataBuffer[0].data[6] = 0x26;
	dataBuffer[0].data[7] = 0x02;
	//市电输入-B相
	dataBuffer[1].data[0] = 0x24;
	dataBuffer[1].data[1] = 0x98;
	dataBuffer[1].data[2] = 0x08;
	dataBuffer[1].data[3] = 0x01;
	dataBuffer[1].data[4] = 0x00;
	dataBuffer[1].data[5] = 0x26;  //c相电压
	dataBuffer[1].data[6] = 0x02;
	dataBuffer[1].data[7] = 0x98;
	//市电输入-C相
	dataBuffer[2].data[0] = 0x34;
	dataBuffer[2].data[1] = 0x08;
	dataBuffer[2].data[2] = 0x01;
	dataBuffer[2].data[3] = 0x00;
	dataBuffer[2].data[4] = 0x26;
	dataBuffer[2].data[5] = 0x02;
	dataBuffer[2].data[6] = 0x00; //A相故障
	dataBuffer[2].data[7] = 0x00; //B相故障
	//市电输入
	dataBuffer[3].data[0] = 0x44;
	dataBuffer[3].data[1] = 0x00; //C相故障
	dataBuffer[3].data[2] = 0x05;
	dataBuffer[3].data[3] = 0x00;

	dataBuffer[3].data[4] = 0x00; //接入和未接入
	dataBuffer[3].data[5] = 0x00; //保护功能
	dataBuffer[3].data[6] = 0x00;
	dataBuffer[3].data[7] = 0x00;
	//取力
	//取力输入-A相
	dataBuffer[4].data[0] = 0x14;
	dataBuffer[4].data[1] += 1;
	dataBuffer[4].data[2] = 0x98;
	dataBuffer[4].data[3] = 0x08;
	dataBuffer[4].data[4] = 0x01;
	dataBuffer[4].data[5] = 0x00;
	dataBuffer[4].data[6] = 0x26;
	dataBuffer[4].data[7] = 0x02;
	//取力输入-B相
	dataBuffer[5].data[0] = 0x24;
	dataBuffer[5].data[1] = 0x98;
	dataBuffer[5].data[2] = 0x08;
	dataBuffer[5].data[3] = 0x01;
	dataBuffer[5].data[4] = 0x00;
	dataBuffer[5].data[5] = 0x26;  //c相电压
	dataBuffer[5].data[6] = 0x02;
	dataBuffer[5].data[7] = 0x98;
	//取力输入-C相
	dataBuffer[6].data[0] = 0x34;
	dataBuffer[6].data[1] = 0x08;
	dataBuffer[6].data[2] = 0x01;
	dataBuffer[6].data[3] = 0x00;
	dataBuffer[6].data[4] = 0x26;
	dataBuffer[6].data[5] = 0x02;
	dataBuffer[6].data[6] = 0x00; //A相故障
	dataBuffer[6].data[7] = 0x00; //B相故障
	//取力输入
	dataBuffer[7].data[0] = 0x44;
	dataBuffer[7].data[1] = 0x00; //C相故障
	dataBuffer[7].data[2] = 0x05;
	dataBuffer[7].data[3] = 0x00;

	dataBuffer[7].data[4] = 0x00; //接入和未接入
	dataBuffer[7].data[5] = 0x00; //保护功能
	dataBuffer[7].data[6] = 0x00;
	dataBuffer[7].data[7] = 0x00;
	//绝缘检测
	dataBuffer[8].data[0] = 0x11;
	dataBuffer[8].data[1] +=1;
	dataBuffer[8].data[2] = 0xFF; //正极母线电阻
	dataBuffer[8].data[3] = 0x00;
	dataBuffer[8].data[4] = 0x01;
	dataBuffer[8].data[5] = 0x00; //
	dataBuffer[8].data[6] = 0x01; //绝缘检测状态标志 0x00正常 0x01异常
	dataBuffer[8].data[7] = 0x00;
	//AD-DC组件上报报文
	dataBuffer[9].data[0] = 0x21;
	dataBuffer[9].data[1] +=1;
	dataBuffer[9].data[2] = 0x98;
	dataBuffer[9].data[3] = 0x08;
	dataBuffer[9].data[4] = 0x01;
	dataBuffer[9].data[5] = 0x00;
	dataBuffer[9].data[6] = 0x00;   //温度
	dataBuffer[9].data[7] = 0x01;   //在位

	dataBuffer[10].data[0] = 0x22;
	dataBuffer[10].data[1] = 0x00;
	dataBuffer[10].data[2] = 0x00;
	dataBuffer[10].data[3] = 0x00;
	dataBuffer[10].data[4] = 0x00;  //接通-保护
	dataBuffer[10].data[5] = 0x00;
	dataBuffer[10].data[6] = 0x00;
	dataBuffer[10].data[7] = 0x00;
	//
	dataBuffer[11].data[0] = 0x21;
	dataBuffer[11].data[1] +=1;
	dataBuffer[11].data[2] = 0x98;
	dataBuffer[11].data[3] = 0x08;

	dataBuffer[11].data[4] = 0x01;
	dataBuffer[11].data[5] = 0x00;

	dataBuffer[11].data[6] = 0x00;   //温度
	dataBuffer[11].data[7] = 0x01;   //在位

	dataBuffer[12].data[0] = 0x22;
	dataBuffer[12].data[1] = 0x00;
	dataBuffer[12].data[2] = 0x00;
	dataBuffer[12].data[3] = 0x00;

	dataBuffer[12].data[4] = 0x00;  //接通-保护
	dataBuffer[12].data[5] = 0x00;
	dataBuffer[12].data[6] = 0x00;
	dataBuffer[12].data[7] = 0x00;

	dataBuffer[13].data[0] = 0x21;
	dataBuffer[13].data[1] +=1;
	dataBuffer[13].data[2] = 0x98;
	dataBuffer[13].data[3] = 0x08;

	dataBuffer[13].data[4] = 0x01;
	dataBuffer[13].data[5] = 0x00;

	dataBuffer[13].data[6] = 0x00;   //温度
	dataBuffer[13].data[7] = 0x01;   //在位

	dataBuffer[14].data[0] = 0x22;
	dataBuffer[14].data[1] = 0x00;
	dataBuffer[14].data[2] = 0x00;
	dataBuffer[14].data[3] = 0x00;

	dataBuffer[14].data[4] = 0x00;  //接通-保护
	dataBuffer[14].data[5] = 0x00;
	dataBuffer[14].data[6] = 0x00;
	dataBuffer[14].data[7] = 0x00;

	//储能输入
	dataBuffer[15].data[0] = 0x11;
	dataBuffer[15].data[1] = 0x00;
	dataBuffer[15].data[2] = 0x05;
	dataBuffer[15].data[3] = 0x00;

	dataBuffer[15].data[4] = 0x00; //接入和未接入
	dataBuffer[15].data[5] = 0x00; //保护功能
	dataBuffer[15].data[6] = 0x00;
	dataBuffer[15].data[7] = 0x00;
}
void Upload_To_Xiankong()
{
//	DataInitForTest();
	dyzhData[0].data[0] = 0x1Bu;
	dyzhData[0].data[1] = serialNum++;
	dyzhData[0].data[2] = dataBuffer[0].data[2];
	dyzhData[0].data[3] = dataBuffer[0].data[3];
	//A相电流
	if(dataBuffer[3].data[4] == 0x00) //市电接入时将电流赋值给取力
	{
		dyzhData[0].data[4] = dataBuffer[0].data[4];
		dyzhData[0].data[5] = dataBuffer[0].data[5];
	}else{
		dyzhData[0].data[4] = 0x00;
		dyzhData[0].data[5] = 0x00;
	}

	dyzhData[0].data[6] = dataBuffer[0].data[6];
	dyzhData[0].data[7] = dataBuffer[0].data[7];

	dyzhData[1].data[0] = 0x2Bu;
	dyzhData[1].data[1] = dataBuffer[1].data[1];
	dyzhData[1].data[2] = dataBuffer[1].data[2];
	//B相电流
	if(dataBuffer[3].data[4] == 0x00) //市电接入
	{
		dyzhData[1].data[3] = dataBuffer[1].data[3];
		dyzhData[1].data[4] = dataBuffer[1].data[4];
	}else{
		dyzhData[1].data[3] = 0x00;
		dyzhData[1].data[4] = 0x00;
	}
	dyzhData[1].data[5] = dataBuffer[1].data[5];
	dyzhData[1].data[6] = dataBuffer[1].data[6];
	dyzhData[1].data[7] = dataBuffer[1].data[7];

	dyzhData[2].data[0] = 0x3Bu;
	dyzhData[2].data[1] = dataBuffer[2].data[1];
	dyzhData[2].data[2] = dataBuffer[2].data[2];
	//C相电流
	if(dataBuffer[3].data[4] == 0x00) //市电接入
	{
		dyzhData[2].data[2] = dataBuffer[2].data[2];
		dyzhData[2].data[3] = dataBuffer[2].data[3];
	}else{
		dyzhData[2].data[2] = 0x00;
		dyzhData[2].data[3] = 0x00;
	}
	dyzhData[2].data[5] = dataBuffer[2].data[5];
	//市电功率
	dyzhData[2].data[6] = dataBuffer[3].data[2];  //功率
	dyzhData[2].data[7] = dataBuffer[3].data[3];  //功率
    //取力输入
	dyzhData[3].data[0] = 0x4Bu;
	dyzhData[3].data[1] = dataBuffer[4].data[2];
	dyzhData[3].data[2] = dataBuffer[4].data[3];
	//A相电流
	if(dataBuffer[7].data[4] == 0x00) //取力接入
	{
		dyzhData[3].data[3] = dataBuffer[0].data[4];
		dyzhData[3].data[4] = dataBuffer[0].data[5];
	}else{
		dyzhData[3].data[3] = 0x00;
		dyzhData[3].data[4] = 0x00;
	}
	dyzhData[3].data[5] = dataBuffer[4].data[6];
	dyzhData[3].data[6] = dataBuffer[4].data[7];
	dyzhData[3].data[7] = dataBuffer[5].data[1];


	dyzhData[4].data[0] = 0x5Bu;
	dyzhData[4].data[1] = dataBuffer[5].data[2];
	//B相电流
	if(dataBuffer[7].data[4] == 0x00) //取力接入
	{
		dyzhData[4].data[2] = dataBuffer[1].data[3];
		dyzhData[4].data[3] = dataBuffer[1].data[4];
	}else{
		dyzhData[4].data[2] = 0x00;
		dyzhData[4].data[3] = 0x00;
	}
	dyzhData[4].data[4] = dataBuffer[5].data[5];
	dyzhData[4].data[5] = dataBuffer[5].data[6];
	dyzhData[4].data[6] = dataBuffer[5].data[7];
	dyzhData[4].data[7] = dataBuffer[6].data[1];

	dyzhData[5].data[0] = 0x6Bu;
	dyzhData[5].data[1] = dataBuffer[6].data[2];
	//C相电流
	if(dataBuffer[7].data[4] == 0x00) //取力接入
	{
		dyzhData[5].data[1] = dataBuffer[2].data[2];
		dyzhData[5].data[2] = dataBuffer[2].data[3];
	}else{
		dyzhData[5].data[1] = 0x00;
		dyzhData[5].data[2] = 0x00;
	}
	dyzhData[5].data[4] = dataBuffer[6].data[5];
	dyzhData[5].data[5] = dataBuffer[7].data[2];
	dyzhData[5].data[6] = dataBuffer[7].data[3];
	dyzhData[5].data[7] = 0x00;
	//储能输入
	dyzhData[6].data[0] = 0x7Bu;
	dyzhData[6].data[1] = dataBuffer[15].data[1];
	dyzhData[6].data[2] = dataBuffer[15].data[2];
	dyzhData[6].data[3] = dataBuffer[15].data[3];
	dyzhData[6].data[4] = dataBuffer[15].data[4];
	dyzhData[6].data[5] = 0x00;
	dyzhData[6].data[6] = dataBuffer[15].data[6];
	dyzhData[6].data[7] = 0x00;
    //
	dyzhData[7].data[0] = 0x8Bu;
	dyzhData[7].data[1] = dataBuffer[2].data[6];
	dyzhData[7].data[2] = dataBuffer[2].data[7];
	dyzhData[7].data[3] = dataBuffer[3].data[1];

	dyzhData[7].data[4] = dataBuffer[6].data[6];
	dyzhData[7].data[5] = dataBuffer[6].data[7];
	dyzhData[7].data[6] = dataBuffer[7].data[1];
	dyzhData[7].data[7] =  ((dataBuffer[3].data[4]&0x03) << 6)|
						   ((dataBuffer[7].data[4]&0x03) << 4)|
						   ((dataBuffer[3].data[6]&0x03) << 2)|
						   (dataBuffer[15].data[7]&0x03);
	//绝缘电阻
	dyzhData[8].data[0] = 0x9Bu;
	dyzhData[8].data[1] = dataBuffer[8].data[2];
	dyzhData[8].data[2] = dataBuffer[8].data[3];
	dyzhData[8].data[3] = dataBuffer[8].data[4];
	dyzhData[8].data[4] = dataBuffer[8].data[5];
	dyzhData[8].data[5] = temperature;
	dyzhData[8].data[6] = temperature>>8;
	//todo:增加整机状态正常、告警、故障
	if(communicationCan1ErrorFlag == 1){
		dyzhData[8].data[7] |= 0x01;
	}else{
		dyzhData[8].data[7] &= 0xfe;
	}
	if((temperature >= 850)&&(temperature <= 1200)){
		//温度告警
		dyzhData[8].data[7] |= 0x40;
	}else if(temperature > 1200){
		//温度故障
		dyzhData[8].data[7] |= 0x20;
	}else{
		dyzhData[8].data[7] &= 0x9f;
	}

//	if(boxState == BSP_FAULT)
//	{
//		//故障
//		dyzhData[8].data[7] |= 0x03;
//
//	}else if(boxState == BSP_ALARM)
//	{
//		//告警
//		dyzhData[8].data[7] |= 0x01;
//
//	}else{
//		//正常
//		dyzhData[8].data[7] &= 0xFC;
//	}
	if((temperature >= 850)&&(temperature <= 1200)){
		dyzhData[8].data[7] |= 0x40;
	}else if(temperature > 1200){
		dyzhData[8].data[2] |= 0x40;
	}else{
		dyzhData[8].data[7] &= 0x0d;
	}
	dyzhData[9].data[0] = 0xABu;
	dyzhData[9].data[1] = 0x00;
	dyzhData[9].data[2] = 0x00;
	dyzhData[9].data[3] = 0x00;
	dyzhData[9].data[4] = 0x00;
	//保护功能状态
	if(dataBuffer[3].data[5]==0x00 &&            //市电
	   dataBuffer[7].data[5]==0x00 &&            //储能
	   (dataBuffer[10].data[4]&0x0c) == 0x00 &&  //充电
	   (dataBuffer[12].data[4]&0x0c) == 0x00 &&  //供电1
	   (dataBuffer[14].data[4]&0x0c) == 0x00 &&  //供电2
	   (dataBuffer[15].data[5]==0x00)            //储能
	)
	{
		dyzhData[9].data[4] = 0x00;
	}else{
		dyzhData[9].data[4] = 0x11;
	}
    //内部通讯状态
	dyzhData[9].data[5] = (communicationErrorFlag & 0xFF);
	dyzhData[9].data[6] = communicationErrorFlag >> 8;
	dyzhData[9].data[7] = dataBuffer[8].data[6];

	dyzhData[10].data[0] = 0xBBu;
	dyzhData[10].data[1] = cumulatetime>>24;
	dyzhData[10].data[2] = cumulatetime>>16;
	dyzhData[10].data[3] = cumulatetime>>8;
	dyzhData[10].data[4] = cumulatetime;
	dyzhData[10].data[5] = 0x00u;//预留
	dyzhData[10].data[6] = 0x00u;//预留

	uint8_t uiCRCNum = 0;
	for(int j = 0;j < 10;j++)
	{
		for(int k = 0;k < 8;k++)
		{
			uiCRCNum += dyzhData[j].data[k];
		}
	}
	for(int j = 0;j < 7;j++)
	{
		uiCRCNum += dyzhData[10].data[j];
	}
	dyzhData[10].data[7] = uiCRCNum;
	for(uint8_t i = 0; i < 11; i ++)
	{
		BSP_CAN1_Transmit(RUNNINGFRAME, dyzhData[i].data);
		HAL_Delay(5);
	}
    /***************运行报文2*********************/
	dyzhData[11].data[0] = 0x15u;
	dyzhData[11].data[1] = serialNum;
	dyzhData[11].data[2] = dataBuffer[9].data[2];
	dyzhData[11].data[3] = dataBuffer[9].data[3];
	dyzhData[11].data[4] = dataBuffer[9].data[4];
	dyzhData[11].data[5] = dataBuffer[9].data[5];
	dyzhData[11].data[6] = dataBuffer[9].data[6];
	// 更新接通关断状态-通讯状态-收报状态
	dyzhData[11].data[7] = dataBuffer[10].data[4];
	if(dataBuffer[9].data[7]==0x01)  //在位
	{
		//通讯正常
		dyzhData[11].data[7] &= 0xEF;
	}else{                            //不在位
		//通讯异常
		dyzhData[11].data[7] |= 0x10;
	}
	dyzhData[12].data[0] = 0x25u;
	dyzhData[12].data[1] = dataBuffer[10].data[1];
	dyzhData[12].data[2] = dataBuffer[10].data[2];
	dyzhData[12].data[3] = dataBuffer[10].data[3];

	dyzhData[12].data[4] = dataBuffer[11].data[2];
	dyzhData[12].data[5] = dataBuffer[11].data[3];
	dyzhData[12].data[6] = dataBuffer[11].data[4];
	dyzhData[12].data[7] = dataBuffer[11].data[5];

	dyzhData[13].data[0] = 0x35u;
	dyzhData[13].data[1] = dataBuffer[11].data[6];
	// 更新接通关断状态-通讯状态-收报状态
	dyzhData[13].data[2] = dataBuffer[12].data[4];
	if(dataBuffer[11].data[7]==0x01)
	{
		//通讯正常
		dyzhData[13].data[2] &= 0xEF;
	}else{
		//通讯异常
		dyzhData[13].data[2] |= 0x10;
	}
    //
	dyzhData[13].data[3] = dataBuffer[12].data[1];
	dyzhData[13].data[4] = dataBuffer[12].data[2];
	dyzhData[13].data[5] = dataBuffer[12].data[3];


	dyzhData[13].data[6] = dataBuffer[13].data[2];
	dyzhData[13].data[7] = dataBuffer[13].data[3];
	dyzhData[14].data[0] = 0x45u;
	dyzhData[14].data[1] = dataBuffer[13].data[4];
	dyzhData[14].data[2] = dataBuffer[13].data[5];
	dyzhData[14].data[3] = dataBuffer[13].data[6];
	// 更新接通关断状态-通讯状态-收报状态
	dyzhData[14].data[4] = dataBuffer[14].data[4];
	if(dataBuffer[13].data[7]==0x01)
	{
		//通讯正常
		dyzhData[14].data[4] &= 0xEF;
	}else{
		//通讯异常
		dyzhData[14].data[4] |= 0x10;
	}
	dyzhData[14].data[5] = dataBuffer[14].data[1];
	dyzhData[14].data[6] = dataBuffer[14].data[2];
	dyzhData[14].data[7] = dataBuffer[14].data[3];

	dyzhData[15].data[0] = 0x55u;
	dyzhData[15].data[1] = 0x00;  //todo:储能控制的接通关断
	dyzhData[15].data[2] = 0x00;
	dyzhData[15].data[3] = 0x00;
	dyzhData[15].data[4] = 0x00;
	dyzhData[15].data[5] = 0x00;
	dyzhData[15].data[6] = 0x00;

	uiCRCNum = 0;
	for(int i = 11;i < 15;i ++){
		for(int j = 0;j < 8;j ++){
			uiCRCNum += dyzhData[i].data[j];
		}
	}
	for(int j = 0;j < 7;j++)
	{
		uiCRCNum += dyzhData[15].data[j];
	}
	dyzhData[15].data[7] = uiCRCNum;
	for(uint8_t i = 11; i < 16; i ++)
	{
		BSP_CAN1_Transmit(RUNNINGFRAME2, dyzhData[i].data);
		HAL_Delay(5);
	}
}

void BSP_CheckModulesComError()
{
	for(int j = 0; j < 7; j ++)
	{
		ErrorID[j].errortimes++;
		if(ErrorID[j].errortimes > JUDEMENTDELAYTIMES)
		{
			receiveFlag &= ~(1<<j);
			ErrorID[j].errortimes = JUDEMENTDELAYTIMES;
		}
		communicationErrorFlag = ~ receiveFlag;
		communicationErrorFlag &= 0x7FF;
	}
}

void Check_ReceiveFlag(uint32_t extId)
{
	uint8_t deviceId = extId & 0xFF;
	receiveFlag |= (1 << (deviceId-1));
	ErrorID[deviceId-1].errortimes = 0;
}

void BSP_UpdateBoxState()
{
    //市电0~3、取力4~7、绝缘8，充电组件9~10.供电组件1:11-12,供电组件2：13-14,储能输入：15
	uint8_t check2 = 0x00;
	uint8_t check1 =
			// 交流
			(dataBuffer[2].data[6])
			| (dataBuffer[2].data[7])
			| (dataBuffer[3].data[1])
			| (dataBuffer[6].data[6])
			| (dataBuffer[6].data[7])
			| (dataBuffer[7].data[1])
			//储能
			| (dataBuffer[15].data[6]);
	if(((check1&0x87) != 0x00))
	{
		//通道上报故障
		boxState = BSP_FAULT;
	}else{
		check2 = (dataBuffer[10].data[1]&0x7E)
				| (dataBuffer[10].data[2]&0xBE)
				| (dataBuffer[10].data[3]&0x3F)
				//供电组件1状态1-3
				| (dataBuffer[12].data[1]&0x7E)
				| (dataBuffer[12].data[2]&0xBE)
				| (dataBuffer[12].data[3]&0x3F)
				//供电组件2状态1-3
				| (dataBuffer[14].data[1]&0x7E)
				| (dataBuffer[14].data[2]&0xBE)
				| (dataBuffer[14].data[3]&0x3F);
		if(check2 != 0x00)
		{
			boxState = BSP_FAULT;
		}else{
			if((check1 & 0x78) != 0x00 || (dyzhData[8].data[7] & 0x04) != 0x00)
			{
				//告警
				//过温告警
//				boxState = BSP_ALARM;
			}
			else
			{
				boxState = BSP_NORMAL;
			}
		}
	}
}

void Configure_Can1_Filter()
{
	CAN_FilterTypeDef sFilterConfig;
	sFilterConfig.FilterBank = 0;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)CONTROLFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =   (((uint32_t)CONTROLFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh = ((((uint32_t)SETTINGFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterMaskIdLow =  (((uint32_t)SETTINGFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan1,&sFilterConfig))
	{
		Error_Handler();
	}

	sFilterConfig.FilterBank = 1;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)READFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =  (((uint32_t)READFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh =  ((((uint32_t)VERSIONFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;;
	sFilterConfig.FilterMaskIdLow =  (((uint32_t)VERSIONFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan1,&sFilterConfig))
	{
		Error_Handler();
	}
}

void Configure_Can2_Filter()
{
	CAN_FilterTypeDef sFilterConfig;

	sFilterConfig.FilterBank = 14;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)0x0c306401<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =   (((uint32_t)0x0c306401<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh = ((((uint32_t)0x0c306402<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterMaskIdLow = (((uint32_t)0x0c306402<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan2,&sFilterConfig))
	{
		Error_Handler();
	}

	sFilterConfig.FilterBank = 15;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)0x0c306403<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =   (((uint32_t)0x0c306403<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh = ((((uint32_t)0x0c306404<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterMaskIdLow = (((uint32_t)0x0c306404<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan2,&sFilterConfig))
	{
		Error_Handler();
	}

	sFilterConfig.FilterBank = 16;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)0x0c306405<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =   (((uint32_t)0x0c306405<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh = ((((uint32_t)0x0c306406<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterMaskIdLow = (((uint32_t)0x0c306406<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan2,&sFilterConfig))
	{
		Error_Handler();
	}

	sFilterConfig.FilterBank = 17;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)0x0c306407<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =   (((uint32_t)0x0c306407<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh = ((((uint32_t)0x0c306408<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterMaskIdLow =  (((uint32_t)0x0c306408<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan2,&sFilterConfig))
	{
		Error_Handler();
	}
	sFilterConfig.FilterBank = 18;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)0x0c306409<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =   (((uint32_t)0x0c306409<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh = ((((uint32_t)0x0c30640A<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterMaskIdLow = (((uint32_t)0x0c30640A<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan2,&sFilterConfig))
	{
		Error_Handler();
	}
	sFilterConfig.FilterBank = 19;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)0x0c30640B<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =   (((uint32_t)0x0c30640B<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh = 0x0000;
	sFilterConfig.FilterMaskIdLow = 0x0000;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan2,&sFilterConfig))
	{
		Error_Handler();
	}

	sFilterConfig.FilterBank = 20;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)SETTINGBACKFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =   (((uint32_t)SETTINGBACKFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh = ((((uint32_t)READBACKFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;//读取报文ID
	sFilterConfig.FilterMaskIdLow =	(((uint32_t)READBACKFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan2,&sFilterConfig))
	{
		Error_Handler();
	}
	sFilterConfig.FilterBank = 21;
	sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST;
	sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
	sFilterConfig.FilterIdHigh = ((((uint32_t)VERSIONCALLBACKFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF0000)>>16;
	sFilterConfig.FilterIdLow =   (((uint32_t)VERSIONCALLBACKFRAME<<3)|CAN_ID_EXT|CAN_RTR_DATA)&0xFFFF;
	sFilterConfig.FilterMaskIdHigh = 0x0000;
	sFilterConfig.FilterMaskIdLow =	0x0000;
	sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
	sFilterConfig.FilterActivation = ENABLE;
	sFilterConfig.SlaveStartFilterBank = 14;

	if(HAL_CAN_ConfigFilter(&hcan2,&sFilterConfig))
	{
		Error_Handler();
	}
}

void BSP_DataInit()
{
	//充电组件9~10.供电组件1:11-12,供电组件2：13-14,储能输入：15
	communicationErrorFlag = 0;

	dataBuffer[3].data[4] = 0x03;    //市电
	dataBuffer[3].data[6] = 0x03;    //插座
	dataBuffer[7].data[4] = 0x03;    //取力


	dataBuffer[10].data[4] = 0x03;   //充电
	dataBuffer[12].data[4] = 0x03;   //供电1
	dataBuffer[13].data[4] = 0x03;   //供电2

	dataBuffer[15].data[7] = 0x03;   //储能
}

#endif
